﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.IO;
using System.Web;
using VelocityShop.Entities;
using VelocityShop.Data;

namespace VelocityShop.Data.SQLServer
{
    public  class SQLServerCatalogProvider:ICatalogProvider 
    {
        public SQLServerCatalogProvider()
        { 
            
        }

        private void SimulateDatabaseAccess()
        {
            System.Threading.Thread.Sleep(5000);
        }

        public Catalog GetCatalog()
        {
            //Simulate load Catalog from DB        

            Catalog c = new Catalog("Catalog1", "EUR");

            c.Categories = new List<Category>();
            c.Categories.Add(new Category{Id = "TV", 
                                          Name = "Televisions",
                                          AvailableTags = new List<ItemTag>{new ItemTag{Tag="promo", Text="Special-Offer"},
                                                                            new ItemTag{Tag="lcd", Text="LCD"},
                                                                            new ItemTag{Tag="plasma", Text="Plasma"},
                                                                            new ItemTag{Tag="led", Text="LED"},
                                                                            new ItemTag{Tag="fullhd", Text="Full-HD"}}
            });

            c.Categories.Add(new Category{Id = "CAM",
                                          Name = "Cameras",
                                          AvailableTags = new List<ItemTag>{new ItemTag{Tag="5", Text="5 MegaPixel"},
                                                                            new ItemTag{Tag="6", Text="6 MegaPixel"},
                                                                            new ItemTag{Tag="7", Text="7 MegaPixel"},
                                                                            new ItemTag{Tag="8", Text="8 MegaPixel"},
                                                                            new ItemTag{Tag="9", Text="9 MegaPixel"},
                                                                            new ItemTag{Tag="10", Text="10 MegaPixel"}}
            });
            c.Categories.Add(new Category{Id = "MP3",
                                          Name = "MP3 Players",
                                          AvailableTags = new List<ItemTag>{new ItemTag{Tag="2", Text="2GB"},
                                                                            new ItemTag{Tag="10", Text="10GB"},
                                                                            new ItemTag{Tag="20", Text="20GB"},
                                                                            new ItemTag{Tag="30", Text="30GB"},
                                                                            new ItemTag{Tag="80", Text="8GB"}}
            });

            
            string[] genres = new string[6] { "arcade", "shooter", "strategy", "simulator", "role-playng", "sport" };

            c.Categories.Add(new Category{Id = "GAM",
                                          Name = "Console & Games",
                                          AvailableTags = new List<ItemTag>{new ItemTag{Tag="arcade", Text="Arcade"},
                                                                            new ItemTag{Tag="shooter", Text="Shooter"},
                                                                            new ItemTag{Tag="strategy", Text="Strategy"},
                                                                            new ItemTag{Tag="simulator", Text="Simulator"},
                                                                            new ItemTag{Tag="role-playng", Text="Role-playng"},
                                                                            new ItemTag{Tag="sport", Text="Sport"}}
            });
            
            return c;
        }

        public IList<CatalogItem> GetItemsByCategoryId(string categoryId)
        {
            SimulateDatabaseAccess();

            switch (categoryId.ToLower())
            {
                case "televisions":
                    return GetTelevisions();
                case "cameras":
                    return GetCameras();
                case "mp3":
                    return GetMP3s();
                case "games":
                    return GetGames();
                default:
                    return new List<CatalogItem>();
            }
        }

        private IList<CatalogItem> GetTelevisions()
        {
            //demo data
            decimal[] prices = new decimal[10] { 780, 899, 1300, 1540, 1199, 1050, 999, 790, 1750, 1199 };
            int[] unitsInStock = new int[10] { 40, 15, 10, 7, 27, 20, 15, 5, 12, 18 };
            int[] inches = new int[5] { 37, 40, 42, 46, 50 };
            string[] technology = new string[3] { "LCD", "PLASMA", "LED"};
            string[] display = new string[6] { "FULL HD", "HD READY", "FULL HD", "HD READY", "FULL HD", "HD READY" };
            string description;
            int imageCounter = 0;
            bool RandomIsPromo = false;
            string itemTags;

            Random rnd = new Random();
            string randomTechnology;

            IList<CatalogItem> allTV = new List<CatalogItem>();

            for (int i = 1; i < 333; i++)
            {
                randomTechnology = technology[rnd.Next(0, 3)];
                description = string.Format("TV Color {0} {1}'' {2} Scart - HDMI - S-Video - USB - Component 2 years warranty", randomTechnology , inches[rnd.Next(0, 4)].ToString(), display[rnd.Next(0, 5)]);

                //cycle image for each product
                if (imageCounter < 19)
                    imageCounter++;
                else
                    imageCounter = 1;

                RandomIsPromo= ((rnd.Next(1, 15) % 5) == 0);

                if (RandomIsPromo)
                    itemTags = randomTechnology + ",PROMO";
                else
                    itemTags = randomTechnology;

                allTV.Add(new CatalogItem
                {
                    ProductId = String.Format("{0}{1}", "TV", i.ToString().PadLeft(4, "0".ToCharArray()[0])),
                    Description = description,
                    Rating = rnd.Next(0, 5),
                    ImageFileName = string.Format("TV_ProductImage{0}.jpg", imageCounter.ToString()),
                    Tags = itemTags,
                    UnitPrice = prices[rnd.Next(0, 9)],
                    UnitsInStock = unitsInStock[rnd.Next(0, 9)],
                    IsPromo = RandomIsPromo
                });


            }

            return allTV;
        
        }

        private IList<CatalogItem> GetCameras()
        {
            //demo data
            decimal[] prices = new decimal[10] { 150, 180, 200, 220, 240, 255, 260, 270, 300, 320 };
            int[] unitsInStock = new int[10] { 27, 15, 10, 8, 27, 20, 15, 5, 12, 18 };
            decimal[] displaySizes = new decimal[4] { 2, 2.5M, 3, 3.5M};
            string[] MegaPixels = new string[6] { "5", "6", "7", "8", "9", "10" };
            string description;
            int imageCounter = 0;

            Random rnd = new Random();
            
            string randomMegaPixels;


            IList<CatalogItem> allCameras = new List<CatalogItem>();

            for (int i = 1; i < 333; i++)
            {
                randomMegaPixels = MegaPixels[rnd.Next(0, 5)];
                description = string.Format("Camera {1} Mega Pixel, display {0}'', USB, 2 years warranty", displaySizes[rnd.Next(0, 4)].ToString(), randomMegaPixels);

                //get always the same image for each product
                if (imageCounter < 19)
                    imageCounter++;
                else
                    imageCounter = 1;

                allCameras.Add(new CatalogItem
                {
                    ProductId = String.Format("{0}{1}", "CAM", i.ToString().PadLeft(4, "0".ToCharArray()[0])),
                    Description = description,
                    Rating = rnd.Next(0, 5),
                    ImageFileName = string.Format("CAM_ProductImage{0}.jpg", imageCounter.ToString()),
                    Tags = randomMegaPixels,
                    UnitPrice = prices[rnd.Next(0, 9)],
                    UnitsInStock = unitsInStock[rnd.Next(0, 9)],
                });

            }

            return allCameras;

        }

        private IList<CatalogItem> GetMP3s()
        {
            //demo data
            decimal[] prices = new decimal[10] { 45, 80, 100, 115, 120, 135, 150, 200, 250, 260 };
            int[] unitsInStock = new int[10] { 10, 75, 50, 36, 27, 20, 15, 5, 12, 18 };
            decimal[] displaySizes = new decimal[4] { 2, 2.5M, 3, 3.5M };
            int[] GigaBytes = new int[5] { 2, 10, 20, 30, 80 };
            string description;
            int imageCounter = 0;

            Random rnd = new Random();
            string randomGigaBytes;

            IList<CatalogItem> allMP3s = new List<CatalogItem>();

            for (int i = 1; i < 333; i++)
            {
                randomGigaBytes = GigaBytes[rnd.Next(0, 5)].ToString();
                description = string.Format("MP3 Player {0} GB, display {1}'', USB, 2 years warranty", randomGigaBytes, displaySizes[rnd.Next(0, 4)].ToString());

                //get always the same image for each product
                if (imageCounter < 19)
                    imageCounter++;
                else
                    imageCounter = 1;

                allMP3s.Add(new CatalogItem
                {
                    ProductId = String.Format("{0}{1}", "MP3", i.ToString().PadLeft(4, "0".ToCharArray()[0])),
                    Description = description,
                    Rating = rnd.Next(0, 5),
                    ImageFileName = string.Format("MP3_ProductImage{0}.jpg", imageCounter.ToString()),
                    Tags =randomGigaBytes,
                    UnitPrice = prices[rnd.Next(0, 9)],
                    UnitsInStock = unitsInStock[rnd.Next(0, 9)],
                });

            }

            return allMP3s;

        }

        private IList<CatalogItem> GetGames()
        {
            //demo data
            decimal[] prices = new decimal[10] { 19.90M, 35, 40, 45, 50, 55, 60, 65, 70, 75 };
            int[] unitsInStock = new int[10] { 40, 15, 10, 7, 27, 20, 15, 5, 12, 18 };
            string[] platforms = new string[6] { "XBOX 360", "PSP", "PS2", "PS3", "Nintendo WII", "PC" };
            string[] genres = new string[6] { "arcade", "shooter", "strategy", "simulator", "role-playng", "sport" };
            string description;
            int imageCounter = 0;

            Random rnd = new Random();
            string randomGenre;

            IList<CatalogItem> allGames = new List<CatalogItem>();

            for (int i = 1; i < 333; i++)
            {
                randomGenre = genres[rnd.Next(0, 5)];
                description = string.Format("Game for {0}, genre: {1}", platforms[rnd.Next(0, 5)], randomGenre);

                //get always the same image for each product
                if (imageCounter < 19)
                    imageCounter++;
                else
                    imageCounter = 1;

                allGames.Add(new CatalogItem
                {
                    ProductId = String.Format("{0}{1}", "GAM", i.ToString().PadLeft(4, "0".ToCharArray()[0])),
                    Description = description,
                    Rating = rnd.Next(0, 5),
                    ImageFileName = string.Format("GAM_ProductImage{0}.jpg", imageCounter.ToString()),
                    Tags=randomGenre,
                    UnitPrice = prices[rnd.Next(0, 9)],
                    UnitsInStock = unitsInStock[rnd.Next(0, 9)],
                });

            }

            return allGames;

        }

        public void InsertCatalogItem(string categoryId, CatalogItem item)
        {
            throw new NotImplementedException();
        }

        public IList<CatalogItem> GetItemsByTag(string categoryId, string tag)
        {
            throw new NotImplementedException();
        }

        public void InsertCatalogItem(string categoryId, CatalogItem item, string tags)
        {
            throw new NotImplementedException();
        }

    }
}
